package com.atguigu.eduservice.controller;


import com.atguigu.baseservice.result.R;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.eduservice.entity.EduVideoInfoForm;
import com.atguigu.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author dayouzi
 * @since 2022-10-18
 */
@Api(description = "课时管理")
@CrossOrigin
@RestController
@RequestMapping("/edu/video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @ApiOperation(value = "新增课时")
    @PostMapping("save-video-info")
    public R save(@ApiParam(name = "videoForm", value = "课时对象", required = true)
                  @RequestBody EduVideoInfoForm eduVideoInfoForm) {
        eduVideoService.saveVideoInfo(eduVideoInfoForm);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询课时")
    @GetMapping("video-info/{id}")
    public R getVideoInfoById(@ApiParam(name = "id", value = "课时id", required = true)
                              @PathVariable String id) {
        EduVideoInfoForm eduVideoInfoForm = eduVideoService.getVideoInfoFormById(id);
        return R.ok().data("item", eduVideoInfoForm);
    }

    @ApiOperation(value = "根据id更新课时")
    @PutMapping("update-video-info/{id}")
    public R getVideoInfoById(@ApiParam(name = "VideoInfoForm", value = "课时对象", required = true)
                              @RequestBody EduVideoInfoForm eduVideoInfoForm,
                              @ApiParam(name = "id", value = "课时id", required = true)
                              @PathVariable String id) {
        eduVideoInfoForm.setId(id);
        eduVideoService.updateVideoInfoById(eduVideoInfoForm);
        return R.ok();
    }

    @ApiOperation(value = "根据id删除课时")
    @DeleteMapping("{id}")
    public R removeVideoInfoById(@ApiParam(name = "id", value = "课时id", required = true)
                              @PathVariable String id) {
        boolean result = eduVideoService.removeVideoInfoById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error(ResultCodeEnum.REMOVE_ERROR);
        }
    }


}

